var Motion=require('../models/motionModel.js');
var code=require('../txtCode/code.js');
var multiparty=require('multiparty');

var fs=require('fs');

module.exports={
	GetListHtml:function(req,res,next){
		if(req.session){
			res.render('motionList',function(err,html){
				if(err) console.error(err);
				res.send(html);
			});
		}//if(req.session)
	},//GetHtml
	GetAddHtml:function(req,res,next){
		if(req.session){
			res.render('motionAdd',function(err,html){
				if(err) console.error(err);
				res.send(html);
			});
		}//if(req.session)
	},//GetHtml
	SaveAdd:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			var oAddMotion=new Motion(data);
			oAddMotion.save().then(
					function(data){
		 			res.send({code:code.success});	
		 			},
		 		function(err){
		 			console.log(err);
		 			res.send({code:code.insertFail});
		 		});//oAddMotion
		}//if(req.session)
	},//SaveAdd
	GetPageQuantity:function(req,res,next){
		if(req.session){
		Motion
		.find()
		.sort()
		.count(function(err,count){
			if(err) console.log(err);
			console.log(count);
			var pageQuantity=Math.ceil(count/10);
			res.send({
				code:code.success,
				data:{'pageQuantity':pageQuantity}});
			});
		}//if(req.session)
	},//GetPageQuantity
	FirstShow:function(req,res,next){
		if(req.session){
			Motion
			.find()
			.sort()
			.limit(10)
			.then(
				function(result){
					res.send({code:code.success,data:result});
				}
				,function(err){
					if(err) console.log(err);
					res.send({code:code.findFail});
				}
				);//
			
		}//if(req.session)

	},//FirstPage
	ShowPage : function(req,res,next){
		if(req.session){
			var PAGE_SIZE=10;
			var condition=req.body.condition;
			var pageNumber=(parseInt(req.body.pageNumber)-1)*PAGE_SIZE;
			Motion
			.find(condition)
			.sort()
			.skip(pageNumber)
			.limit(10)
			.then(function(result){
				console.log(result);
				res.send({
					code:[1000,'success'],
					data:result
				});
			});
		}//if(req.session)
	},//ShowPage
	FetchMotion:function(req,res,next){
		if(req.session){
			var needMotionname=req.body.needMotionname;
			Motion
			.find({motionname:needMotionname})
			.then(function(result){
				res.send({
					code:[1000,'success'],
					data:result
				})

			})
		}//if(req.session)
	},//FetchMotion
	MotionBaseInfo:function(req,res,next){
		if(req.session){
			var accountBaseInfo=JSON.parse(req.query.needMotion);
			console.log(typeof accountBaseInfo);
			console.log(accountBaseInfo);
			res.render('motionBaseInfo',accountBaseInfo,function(err,html){
				if(err) console.log(err);
				res.send(html);
			});
		}//if(req.session)
	},//MotionBaseInfo
	searchMotionThree:function(req,res,next){
		if(req.session){
			var ThreeData=req.body;
			console.log(ThreeData);
			//删选条件的选择问题
			if(ThreeData.motionname){
				Motion
				.find({motionname:ThreeData.motionname})
				.then(function(result){
					res.send({
						code:[1000,'success'],
						data:result           //此为一个数组
					});
					return;
				});
			};

			if(ThreeData.difficulty[0] && !ThreeData.motionRegion){
				Motion
				.find()
				.where({difficulty:['1',null,null]})
				.then(function(result){
					res.send({
						code:[1000,'success'],
						data:result
					});
					return;
				})
			}//if(ThreeData.difficulty[0])

			if(ThreeData.difficulty[1] && !ThreeData.motionRegion){
				Motion
				.find()
				.where({difficulty:[null,'2',null]})
				.then(function(result){
					res.send({
						code:[1000,'success'],
						data:result
					});
					return;
				})
			}//if(ThreeData.difficulty[1])

			if(ThreeData.difficulty[2] && !ThreeData.motionRegion){
				Motion
				.find()
				.where({difficulty:[null,null,'3']})
				.then(function(result){
					res.send({
						code:[1000,'success'],
						data:result
					});
					return;
				})
			}//if(ThreeData.difficulty[2])

			if(ThreeData.difficulty[0] && ThreeData.motionRegion){
				Motion
				.find()
				.where({difficulty:['1',null,null],motionRegion:ThreeData.motionRegion})
				.then(function(result){
					res.send({
						code:[1000,'success'],
						data:result
					});
					return;
				})
			}//if(ThreeData.difficulty[0])

			if(ThreeData.difficulty[1] && ThreeData.motionRegion){
				Motion
				.find()
				.where({difficulty:[null,'2',null],motionRegion:ThreeData.motionRegion})
				.then(function(result){
					res.send({
						code:[1000,'success'],
						data:result
					});
					return;
				})
			}//if(ThreeData.difficulty[1])

			if(ThreeData.difficulty[2] && ThreeData.motionRegion){
				Motion
				.find()
				.where({difficulty:[null,null,'3'],motionRegion:ThreeData.motionRegion})
				.then(function(result){
					res.send({
						code:[1000,'success'],
						data:result
					});
					return;
				})
			}//if(ThreeData.difficulty[2])

			// if(ThreeData.motionRegion){
				Motion
				.find({motionRegion:ThreeData.motionRegion})
				.then(function(result){
					res.send({
						code:[1000,'success'],
						data:result
					});//res.send
				});//Motion
			// };//if(ThreeData.motionRegion)

		}//if(req.session)
	},//searchMotionThree
	SearchMotion:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			Motion
			.find(data)
			.then(function(data){
				res.send({
					data:data
				});
			},function(err){
				if(err) console.log(err);
			});
		}//if(req.session)
	},//SearchMotion
	PageQuantity:function(req,res,next){
		if(req.session){
			var dataRec=req.body;
			var condition=dataRec;
			Motion
			.find(condition)
			.sort()
			.count(function(err,count){
				if(err) console.log(err);
				console.log(count);
				var pageQuantity=Math.ceil(count/10);
				res.send({
					code:code.success,
					data:{'pageQuantity':pageQuantity}});
				});
		};//if(req.session)
	},//PageQuantity()
	PageContent:function(req,res,next){
		if(req.session){
			var PAGE_SIZE=10;
			var condition=req.body.condition;
			var pageNumber=(parseInt(req.body.pageNumber)-1)*PAGE_SIZE;
			Motion
			.find(condition)
			.sort()
			.skip(pageNumber)
			.limit(10)
			.then(function(result){
				console.log(result);
				res.send({
					code:[1000,'success'],
					data:result
				});
			});
		};//if(req.session)
	},//PageContent()
	ConditionMotion:function(req,res,next){
		if(req.session){
			var condition=req.body;
			Motion
			.find(condition)
			.then(function(data){
				res.send({
					code:[1000,'success'],
					data:data
				});
			})//
		}//if(req.session)
	},//ConditionMotion
	PictureTest:function(req,res,next){
		if(req.session){
		
			var form=new multiparty.Form();

			form.encoding='utf-8';
			// form.maxFilesSize=2*1024*1024;
			// form.parse(req,function(err,fields,files){
			// 	if(err) console.log(err);
			// 	console.log(files.originalFileName);
			// 	console.log(files.path);
			// 	res.send({
			// 		originalFileName:files.originalFileName,
			// 		path:files.path
			// 	});

			// });

			form.on('file',function(name,file){
			});

			form.uploadDir='/root/bully/smartGym/public/images/motions';

			form.parse(req,function(err,fields,files){
				if(err) {
					console.log(err);
					return;
				}
				console.log(files.motionPicture[0].path);
				var zRename='/root/bully/smartGym/public/images/motions/'+fields.number[0]+'.JPG';
				fs.rename(files.motionPicture[0].path,zRename);
				console.log(files.motionPicture);

				console.log(fields.mainRegion);

				//整理数据
				motionname=fields.motionname[0];
				number=fields.number[0];
				instruction=fields.instruction[0];
				motionPoint=fields.motionPoint[0];
				difficulty=fields.difficulty[0];

				machine=fields.machine[0];
				beat=fields.beat[0];

				mainRegion=new Array(6);
				secondaryRegion=new Array(6);
				target=new Array(5);

				trans(mainRegion,fields.mainRegion);
				trans(secondaryRegion,fields.secondaryRegion);
				trans(target,fields.target);

				function trans(dbD,recD){
					recD.forEach(function(v,i){
						v=v.split(',');
						dbD[parseInt(v[1])]=v[0];
					});//forEach()
				}
				
				
				motionPicture=zRename;  //图片地址

				//存进数据库中
				var data={
							motionname : motionname,
							number:number,
							instruction :instruction,
							motionPoint :motionPoint ,
							difficulty :difficulty,

							machine :machine,
							beat :beat ,

							mainRegion :mainRegion,
							secondaryRegion :secondaryRegion,
							target :target ,
				
							motionPicture:motionPicture
						}
				var oAddMotion=new Motion(data);
				oAddMotion.save().then(
					function(data){

						res.render('back',function(err,html){
							res.send(html);
						});

		 			
		 			},
		 		function(err){
		 			console.log(err);
		 			res.send({code:code.insertFail});
		 		});//oAddMotion
				//存进数据库中

				// res.send({files:files,fields:fields});

			});//form.parse
		}//if(req.session)
	},//PictureTest
	CheckChongming:function(req,res,next){
		if(req.session){
			console.log(req.body);
			var data=req.body;
			if(data.motionname || data.number){
			Motion
			.find(data)
			.then(function(result){
				console.log(result);
				if(result.length==0){
					res.send({
						code:1000
					});
				}else{
					res.send({
						code:1001
					})
				}//if(result.length)
			});//then
		};//if(data.motionname || data.number)
		};//if(req.session)
	},//CheckChongming

}

	
		